package com.glodon.paas.account.web.resource.internal;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.glodon.paas.account.api.bean.Communicate;
import com.glodon.paas.account.biz.impl.CommunicateBookBizImpl;
import com.glodon.paas.account.dao.domain.CommunicateEntity;
@Component
@Path("/internal/communicateBook")
public class CommunicateBookResourceImpl{
	@Autowired
	private CommunicateBookBizImpl communicateBookBiz;
 
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/userId/{userId}/contactId/{contactId}/remarkName/{remarkName}")
	public String addCommunicate(
			@PathParam("userId")String userId,			// 用户ID
			@PathParam("contactId")String contactId,		// 联系人ID
			@PathParam("remarkName")String remarkName		// 用户备注的联系人姓名,默认使用Email
			){
		CommunicateEntity communicateEntity = new CommunicateEntity();	
		communicateEntity.setUserId(userId);
		communicateEntity.setContactId(contactId);
		communicateEntity.setRemarkName(remarkName);		
		return communicateBookBiz.add(communicateEntity, true);
	}

	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/{id}")
	public Integer removeById(
			@PathParam("id")String id
			){
		return communicateBookBiz.removeById(id);		
	}
	
	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/userId/{userId}/contactId/{contactId}")
	public Integer remove(
			@PathParam("userId")String userId,			// 用户ID
			@PathParam("contactId")String contactId		// 联系人ID 
			){		 
		int id = communicateBookBiz.remove(userId, contactId);		
		return id;
	}
	
	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/order/id/{id}/order/{newOrder}/{orderType}")
	public Integer updateOrder(
			@PathParam("id")String id, 				// id
			@PathParam("newOrder")Integer newOrder, 		// 新序号
			@PathParam("orderType")Integer orderType		// 排列顺序，1递增，-1递减
			){
		return communicateBookBiz.updateOrder(id, newOrder, orderType);
	}
	
	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/remark/id/{id}/remark/{remarkName}/{remarkText}")
	public Integer updateRemark(
			@PathParam("id")String id,
			@PathParam("remarkName")String remarkName,
			@PathParam("remarkText")String remarkText
			){ 
		return communicateBookBiz.updateRemark(id, remarkName, remarkText);
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/count/{userId}")
	public String count(
			@PathParam("userId")String userId
			){
		return String.valueOf(communicateBookBiz.count(userId));
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/{id}")
	public Communicate queryById(
			@PathParam("id")String id
			){
		return converteToDto(communicateBookBiz.queryById(id), true);
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/userId/{userId}/contactId/{contactId}")
	public Communicate queryByContactId(
			@PathParam("userId")String userId,
			@PathParam("contactId")String contactId
			){
		return converteToDto(communicateBookBiz.queryByContactId(userId, contactId), true);
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/userId/{userId}")
	public List<Communicate> query(
			@PathParam("userId")String userId,
			@MatrixParam("orderType")@DefaultValue("1")Integer orderType,
			@MatrixParam("startIndex") @DefaultValue("0") Integer startIndex,
			@MatrixParam("count")@DefaultValue("20")Integer count
			){
		return converteToDto(communicateBookBiz.query(userId, orderType, startIndex, count));
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/judge/userId/{userId}/contactId/{contactId}")
	public Integer judge(
			String userId,
			String contactId
			){
		return communicateBookBiz.judge(userId, contactId);
	}
		
	private List<Communicate> converteToDto(List<CommunicateEntity> domainList) { 
		List<Communicate> list = new ArrayList<Communicate>();
		for(CommunicateEntity item : domainList){
			list.add(converteToDto(item, false));
		}
		// 批量查询填充relationEmail信息
		// ...
		return list;
	}
	
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/contactId/{userId}")
	public ArrayList<String> contactId(
			@PathParam("userId")String userId,
			@MatrixParam("orderType")@DefaultValue("1")Integer orderType,
			@MatrixParam("startIndex") @DefaultValue("0") Integer startIndex,
			@MatrixParam("count")@DefaultValue("2000")Integer count
			){
		ArrayList<String> ids = new ArrayList<String>();
		List<CommunicateEntity> list = communicateBookBiz.query(userId, orderType, startIndex, count);
		for(CommunicateEntity en : list){
			ids.add(en.getContactId());
		}
		return ids;
	}
	
	private Communicate converteToDto(CommunicateEntity domain, boolean verifyEmail) { 
		if(domain == null){
			return null;
		}
		Communicate dto = new Communicate();
		dto.setCreateTime(domain.getCreateTime());
		dto.setId(domain.getId());
		dto.setOrderNum(domain.getOrderNum());		
		dto.setContactEmail(domain.getContactEmail());
		dto.setContactId(domain.getContactId());
		dto.setRemarkName(domain.getRemarkName());
		dto.setRemarkText(domain.getRemarkText());
		dto.setUserId(domain.getUserId());
		dto.setUpdateTime(domain.getUpdateTime());
		//TODO Email取自account
		if(verifyEmail){
			
		}
		return dto;
	}
}
